package disruptor;

import netty.util.StartAndStop;
import org.jctools.queues.MpscBlockingConsumerArrayQueue;
import test.PerformanceAnalyze;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class JcToolQueue extends StartAndStop {
    MpscBlockingConsumerArrayQueue<Long> queue = new MpscBlockingConsumerArrayQueue<>(64);

    Executor executor = Executors.newFixedThreadPool(1);

    PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    public void start() {
        executor.execute(() -> {
            try {
                while (running) {
                    Long time = queue.take();

                    performanceAnalyze.delay(System.nanoTime() - time);
                    performanceAnalyze.meter();
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        while (running) {
            queue.offer(System.nanoTime());
        }
    }

    public static void main(String[] args) {
        new JcToolQueue().start();

    }
}
